﻿using iText.Kernel.Pdf;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.IO;
using System.Windows;

namespace PdfMod.ViewModels
{
    public class PdfMainViewModel : BindableBase
    {
        public string Title { get; set; } = "PdfMod";
        public DelegateCommand ModifyPdfMateCommand { get; set; }
        public DelegateCommand ModifyPdfInDirMateCommand { get; set; }

        public PdfMainViewModel()
        {
            ModifyPdfMateCommand = new DelegateCommand(OnModifyPdfMate);
            ModifyPdfInDirMateCommand = new DelegateCommand(OnModifyPdfInDirMate);
        }

        private void OnModifyPdfInDirMate()
        {
            var dialog = new OpenFolderDialog()
            {
                Title = "Selected Pdf dir",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                Multiselect = false
            };

            string folderName = "";
            if (dialog.ShowDialog() == true)
            {
                folderName = dialog.FolderName;
            }
            if (folderName != "")
            {
                GetDirectoryInfo(folderName);
            }
            MessageBox.Show("Clear OK");
        }

        private void GetDirectoryInfo(string rootDir)
        {
            var dir = new DirectoryInfo(rootDir);
            var files = dir.GetFiles("*.pdf");
            if (files != null)
            {
                foreach (var file in files)
                {
                    ClearMateInfo(file.FullName);
                }
            }
            var childDirs = dir.GetDirectories();
            foreach (var childDir in childDirs)
            {
                GetDirectoryInfo(childDir.FullName);
            }
        }

        private void OnModifyPdfMate()
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "PDF file (*.pdf)|*.pdf";
            ofd.Multiselect = false;
            ofd.Title = "Selected a PDF file";
            if (ofd.ShowDialog() == true)
            {
                var fileName = ofd.FileName;
                ClearMateInfo(fileName);
            }
        }

        public byte[] SetCustomMetadata(byte[] pdf, string key = "", string value = "")
        {
            try
            {
                using var inputStream = new MemoryStream(pdf);
                using var reader = new PdfReader(inputStream);
                using var outputStream = new MemoryStream();
                using var writer = new PdfWriter(outputStream);
                using (var document = new PdfDocument(reader, writer))
                {
                    var documentInfo = document.GetDocumentInfo();
                    documentInfo.SetAuthor("");
                    documentInfo.SetTitle("");
                    documentInfo.SetSubject("");
                    documentInfo.SetCreator("");
                    documentInfo.SetKeywords("");
                    documentInfo.SetProducer("");
                    documentInfo.SetMoreInfo(key, value);
                }

                // The PdfDocument must be closed first to write to the output stream
                return outputStream.ToArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void ClearMateInfo(string fileName)
        {
            try
            {
                var bytes = File.ReadAllBytes(fileName);
                var newBytes = SetCustomMetadata(bytes);
                if (newBytes != null)
                {
                    File.WriteAllBytes(fileName, newBytes);
                }
                //PdfDocument pdf = new PdfDocument(new PdfWriter(fileName, new WriterProperties()));
                //Document document = new Document(pdf);
                //PdfDocumentInfo info = pdf.GetDocumentInfo();
                //if (info != null)
                //{
                //    info.SetTitle("");
                //    info.SetAuthor("");

                //}
                //document.Close();
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }
        }
    }
}